<template>
    <div>
        <h2>个人资料</h2>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
            <el-form-item label="姓名：">
                <el-input v-model="form.admin_name" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="部门：">
                <el-input v-model="form.depart_name" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="角色：">
                <el-input v-model="form.role_name" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item
                    label="手机号："
                    prop="admin_phone"
                    :rules="[{required:true}]"
            >
                <el-input v-model.number="form.admin_phone"></el-input>
            </el-form-item>
            <el-form-item
                    prop="email"
                    label="邮箱："
                    :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]"
            >
                <el-input v-model="form.email"></el-input>
            </el-form-item>
            <div>
                <label>头像：</label>
                <el-upload
                        style="text-align: center"
                        class="avatar-uploader"
                        action="http://172.16.10.70:8080/"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </div>
            <el-form-item>
                <el-button style="margin-left: 50px;margin-top: 50px" type="primary" @click="Save">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name:'Predata',
        data() {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    console.log(reg.test(value));
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            };
            return {
                imageUrl: '',
                form: {
                    admin_name:'范冰冰',
                    depart_name:'编辑部',
                    role_name:'主编',
                    admin_phone:'12345678912',
                    email:'123456789@qq.com',
                },
                rules:{
                    admin_phone:[
                        {validator:checkPhone,trigger:'blur'}
                    ]
                },
                dialogImageUrl: '',
                dialogVisible: false
            }
        },
        methods: {
            Save() {
                console.log('submit!');
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style lang="less" scoped>
    form{
        max-width: 350px;
        margin: 0 auto;
    }
    h2{
        max-width: 350px;
        text-align: center;
        margin: 25px auto;
    }
    /*.el-input{*/
    /*    border: 0 none;*/
    /*    outline-style: none;*/
    /*    background-color: transparent;*/
    /*}*/
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 100px;
        height: 100px;
        line-height: 100px;
        text-align: center;
        background-color: white;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>